package com.example.zuoye.mvp.view;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.mylibrary.mvp.view.BaseActivity;
import com.example.zuoye.R;
import com.example.zuoye.adapter.ViewPageAdapter;
import com.example.zuoye.bean.TagTravel;
import com.example.zuoye.bean.UserBean;
import com.example.zuoye.mvp.contract.MainContract;
import com.example.zuoye.mvp.model.MainModel;
import com.example.zuoye.mvp.presenter.MainPresenter;
import com.google.android.material.tabs.TabLayout;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.List;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {
    private Toolbar toor;
    private TabLayout tl;
    private ViewPager vp;
    private SlidingMenu slidingMenu;
    ImageView iv;
    @Override
    public int bondLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        toor = (Toolbar) findViewById(R.id.toor);
        tl = (TabLayout) findViewById(R.id.tl);
        vp = (ViewPager) findViewById(R.id.vp);
    }

    @Override
    public void initData() {
        mPresenter=new MainPresenter(new MainModel(),this);
        mPresenter.requestTag();
        initViewPage();


        //抽屉
        toor.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.showMenu();
            }
        });
        slidingMenu=new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setBehindOffset(50);
        slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
        View inflate = LayoutInflater.from(this).inflate(R.layout.sliding, null);
        Button bt1 = inflate.findViewById(R.id.bt1);
        Button bt2 = inflate.findViewById(R.id.bt2);
        iv = inflate.findViewById(R.id.iv);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.showContent();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,BodyActivity.class));
            }
        });

        //换头像
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this,TouActivity.class),100);
            }
        });

        slidingMenu.setMenu(inflate);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100&&resultCode==RESULT_OK){
            if(SPUtils.getInstance().getString("img")!=null){
                Glide.with(this).load(SPUtils.getInstance().getString("img"))
                        .transform(new CircleCrop())
                        .into(iv);
            }
        }


    }

    @Override
    public void initViewPage() {
        ViewPageAdapter viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager());
        vp.setAdapter(viewPageAdapter);
        tl.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void initAdapter(List<UserBean> list) {
        for (int i = 0; i < list.size(); i++) {
            tl.addTab(tl.newTab().setText(list.get(i).getTag_name()));
        }
    }

    @Override
    public void loadLayout() {

    }

    @Override
    public void hideLayout() {

    }
}
